package com.cmcc.waf.report.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cmcc.waf.report.feign.model.ResponseVO;
import com.cmcc.waf.report.feign.model.WebSafeInfoVO;
import com.cmcc.waf.report.feign.model.WebSafeRequest;
import com.cmcc.waf.report.service.AttackCountService;
import com.cmcc.waf.report.utils.EcloudResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @author waf
 */
@RestController
@Slf4j
public class AttackCountController {

    @Autowired
    private AttackCountService service;

    /**
     * 攻击总次数
     * @param request
     * @return
     */
    @PostMapping("/attack-total")
    public EcloudResult<Object> attackTotal(@RequestBody WebSafeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<Object> response = service.getTotal(request);
        if (response.success()) {
            return new EcloudResult<>().ok(response.getBody());
        } else {
            return new EcloudResult<>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }

    /**
     * 攻击趋势
     * @param request
     * @return
     */
    @PostMapping("/attack-count")
    public EcloudResult<List<WebSafeInfoVO.AlarmTrend>> attackCount(@RequestBody WebSafeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<List<WebSafeInfoVO.AlarmTrend>> response = service.getCount(request);
        if (response.success()) {
            return new EcloudResult<List<WebSafeInfoVO.AlarmTrend>>().ok(response.getBody());
        } else {
            return new EcloudResult<List<WebSafeInfoVO.AlarmTrend>>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }

}
